# -*- coding: utf-8 -*-
'''
Created on Oct 30, 2010

@author: HoangNamHai
'''
import urllib
import cStringIO as StringIO
from lxml import etree
from pymongo import Connection
import datetime

stringify = etree.XPath("string()")
connection = Connection('localhost', 27017)
db = connection['lichtv']
collection = db['vnexpress']

stringify = etree.XPath("string()")
channels = {'VTV1':'1','VTV2':'2','VTV3':'3','VTV4':'4','VTV6':'5','HTV1':'6','HTV2':'7','HTV3':'8','HTV4':'9','HTV7':'10','HTV9':'11','Thuần Việt':'12','HTVC - MUSIC':'13','HTVC - MOVIE':'14','VietnamNet':'15','HanoiTV':'16','TVB8':'17','Disney':'18','Du lịch':'19','CNN':'20','Bloomberg':'21','Star Movies':'22','NHK World':'23','DW-TV':'24','Arirang':'25','News Asia':'26','Fashion TV':'27','TV5MONDE':'28','Star World':'29','FBNC':'30','Phoenix Info':'31','Star Sport':'32','ESPN':'33','BTV3':'34','BTV5':'35','Animax':'36','Channel [V]':'37','Vĩnh Long':'38','HTVC - Phụ nữ':'39','HTVC - Gia đình':'40','Đồng Nai 1':'41','Đồng Nai 2':'42','BTV1':'43','BTV2':'44','BTV4':'45','NOW-TV':'46','Playhouse Disney':'47','O2TV':'48','BTV9':'49','CCTV5':'50','VTV9':'51','iTV':'52','Australia NWK':'53','Cần Thơ':'54','HBO':'55','Cinemax':'56','VCTV3':'57','VCTV7':'58','Boomerang':'59','DW-TV Asia+':'60','Hải Phòng':'61','Cartoon Network':'62','VTC1':'63','VTC2':'64','VTC3':'65','VTC7':'66','VTC9':'67','Astro Cảm xúc':'68','Info TV':'69','VTC8':'70','VTC10':'71','Super Sport 1':'72','Super Sport 2':'73','Super Sport 3':'74','VBC':'100',}
for chName in channels.keys():
    try:
        print chName
        print 'http://vnexpress.net/ListFile/iTV/{0}.xml'.format(channels[chName])
        result = urllib.urlopen('http://vnexpress.net/ListFile/iTV/{0}.xml'.format(channels[chName]))
        html = result.read()
        parser = etree.XMLParser(encoding='utf-8', strip_cdata=False)
        tree = etree.parse(StringIO.StringIO(html), parser)
        # print html
        
        result = {}
        result['date'] = stringify(tree.xpath('//Date')[0]).encode('utf-8')
        result['name'] = chName
        result['_id'] = hash(chName)
        result['lastupdate'] = str(datetime.datetime.now())
    
        data = []
        for row in tree.xpath('//Items/Item'):
            dataRow = {}
            dataRow['time'] = stringify(row.xpath('./TVTime')[0]).encode('utf-8')
            dataRow['desc'] = stringify(row.xpath('./Desc')[0]).encode('utf-8')
            print dataRow
            data.append(dataRow)
        result['data'] = data
        print result            
        collection.save(result)
    except:
        print "------- ERROR ---------------"
        raise
print "Đã xong", datetime.datetime.now()